<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>注册新用户</title>
    <style>
        h1{color: red;}
        input{padding: 6px;margin: 6px;border-radius: 6px;}
        .msg{color: red;padding: 20px 0;}
    </style>
</head>
<body><?php
if($_POST){
    try {
        $db=new PDO("mysql:host=localhost; dbname=db2;", 'root', '12qwas');
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
        $ps=$db->prepare('insert into students (xh,name,tel) value (?,?,?)');
        $ps->execute([$_POST['xh'],$_POST['name'],$_POST['tel']]);
        header(header:'Location:index.php');
        return;
    }catch(PDOException $e){
        $msg=$e->getMessage();
    }
}
?>
<h1>注册新用户</h1>
<form method="post">
    学号：<input type="text" name="xh" value="<?=$_POST['xh']?>"><br>
    姓名：<input type="text" name="name" value="<?=$_POST['name']?>"><br>
    电话：<input type="text" name="tel" value="<?=$_POST['tel']?>"><br>
    <div class="msg"><?=$msg?></div>
    <input type="submit" value="提交数据">
</form>
</body>
</html>

